package dao.impl;


import dao.NoteDao;
import dao.PageDao;
import dao.UserDao;
import pojo.Note;
import pojo.NoteLabels;
import pojo.User;
import service.NoteLabelsService;
import service.UserService;
import service.impl.NoteLabelsServiceImpl;
import service.impl.UserServiceImpl;
import util.DataUtils;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;

public class PageDaoImpl implements PageDao {
    private Connection conn = null;
    private ResultSet rs = null;


    public Integer totalNotes() throws SQLException {
        Integer total = 0;
        NoteDao noteDao = new NoteDaoImpl();
        total = noteDao.totalNotes();
        return total;
    }


    public ArrayList<Note> list(Integer start, Integer rows) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        ArrayList<Note> list = new ArrayList<>();
        String sql = "select * from biji  where style='" + 1 + "' limit " + start + "," + rows + "";
        rs = JdbcUtils.selectsql(sql);
//        while (true) {
//            if (!rs.next()) break;
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        //System.out.println(row);
        //ResultSetMetaData metaData = rs.getMetaData();
        //metaData.getColumnCount();
        //System.out.println(rs.getRow());
//        System.out.println(rs.getMetaData().getColumnCount());
        do {
            //System.out.println(rs.getRow());
            row--;
            Note note = new Note();
            note = (Note) DataUtils.dataEncapsulation(Class.forName("pojo.Note"), rs);
            list.add(note);
            //} while (num < rs.getRow());
        } while (row > 0);
        //}
        JdbcUtils.closeconn(rs, null, conn);
        return list;
    }


    public Integer areaTotalNotes(String type) throws SQLException {
        Integer total = 0;
        NoteDao noteDao = new NoteDaoImpl();
        total = noteDao.AreaTotalNotes(type);
        return total;
    }


    public ArrayList<Note> areaNoteList(String type, Integer start, Integer rows) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        ArrayList<Note> list = new ArrayList<>();
        String sql = "select * from biji where type='" + type + "' and style='" + 1 + "' limit " + start + "," + rows + "";
        rs = JdbcUtils.selectsql(sql);
//        while (true) {
//            if (!rs.next()) break;
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        do {
            row--;
            Note note = new Note();
            note = (Note) DataUtils.dataEncapsulation(Class.forName("pojo.Note"), rs);
            list.add(note);
        } while (row > 0);
        //}
        JdbcUtils.closeconn(rs, null, conn);
        return list;
    }

    public Integer userTotalNotes(String usename) throws SQLException {
        Integer total = 0;
        NoteDao noteDao = new NoteDaoImpl();
        total = noteDao.userTotalNote(usename);
        return total;
    }


    public ArrayList<Note> userNoteList(String usename, Integer start, Integer rows) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        ArrayList<Note> list = new ArrayList<>();
        String sql = "select * from biji where usename='" + usename + "' and style='" + 1 + "' limit " + start + "," + rows + "";
        rs = JdbcUtils.selectsql(sql);
//        while (true) {
//            if (!rs.next()) break;
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        do {
            row--;
            Note note = new Note();
            note = (Note) DataUtils.dataEncapsulation(Class.forName("pojo.Note"), rs);
            list.add(note);
        } while (row > 0);
        //}
        JdbcUtils.closeconn(rs, null, conn);
        return list;
    }


    public Integer userCheckedNotes(String usename) throws SQLException {
        Integer total = 0;
        NoteDao noteDao = new NoteDaoImpl();
        total = noteDao.userTotalCheckedNote(usename);
        return total;
    }


    public ArrayList<Note> userCheckedNoteList(String usename, Integer start, Integer rows) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        ArrayList<Note> list = new ArrayList<>();
        String sql = "select * from biji where usename='" + usename + "' and style='" + 0 + "' limit " + start + "," + rows + "";
        rs = JdbcUtils.selectsql(sql);
//        while (true) {
//            if (!rs.next()) break;
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        do {
            row--;
            Note note = new Note();
            note = (Note) DataUtils.dataEncapsulation(Class.forName("pojo.Note"), rs);
            list.add(note);
        } while (row > 0);
        //}
        JdbcUtils.closeconn(rs, null, conn);
        return list;
    }


    public Integer userTypeNotes(String usename, String type) throws SQLException {
        Integer total = 0;
        NoteDao noteDao = new NoteDaoImpl();
        total = noteDao.userAreaTotalNotes(usename, type);
        return total;
    }

    /**
     * wrong
     * 分页查询每页记录--用户区域笔记
     *
     * @param type
     * @param
     * @param
     * @return
     */
//    public ArrayList<Note> userAreaNoteList(String usename,String type,Integer start,Integer rows){
//        ArrayList<Note> list=new ArrayList<>();
//        JDBC jdbc=new JDBC();
//        Connection conn = null;
//        try {
//            conn=jdbc.dbconn();
//        } catch (SQLException e) {
//            e.prIntegerStackTrace();
//        }
//        String sql="select * from biji where type='"+type+"' and usename='"+usename+"' and style='"+1+"' limit "+start+","+rows+"";
//        ResultSet rs=jdbc.selectsql(sql);
//        while(true) {
//            try {
//                if (!rs.next()) break;
//                    Note note=new Note();
//                    note.setStyle(rs.getInteger("style"));
//                    note.setNid(rs.getInteger("nid"));
//                    note.setTitle(rs.getString("title"));
//                    note.setContent(rs.getString("content"));
//                    note.setType(rs.getString("type"));
//                    note.setUsename(rs.getString("usename"));
//                    note.setLikes(rs.getInteger("likes"));
//                    note.setFavourite(rs.getInteger("favourite"));
//                    list.add(note);
//            } catch (SQLException e) {
//                e.prIntegerStackTrace();
//            }
//        }
//        jdbc.closeconn(rs,null,conn);
//        return list;
//    }
    public Integer areaTotalCheckedNotes(String type) throws SQLException {
        Integer total = 0;
        NoteDao noteDao = new NoteDaoImpl();
        total = noteDao.areaCheckedNotes(type);
        return total;
    }


    public ArrayList<Note> areaCheckedNoteList(String type, Integer start, Integer rows) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        ArrayList<Note> list = new ArrayList<>();
        String sql = "select * from biji where type='" + type + "' and style='" + 0 + "' limit " + start + "," + rows + "";
        rs = JdbcUtils.selectsql(sql);
//        while (true) {
//            if (!rs.next()) break;
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        do {
            row--;
            Note note = new Note();
            note = (Note) DataUtils.dataEncapsulation(Class.forName("pojo.Note"), rs);
            list.add(note);
        } while (row > 0);
        //}
        JdbcUtils.closeconn(rs, null, conn);
        return list;
    }


    public Integer areaTotalUsers(String type) throws SQLException {
        Integer total = 0;
        HashSet<String> areaUsers = new HashSet<String>();
        UserDao userDao = new UserDaoImpl();
        areaUsers = userDao.areaUserUsename(type);
        total = areaUsers.size();
        return total;
    }


    public ArrayList<Note> areaUsersList(HashSet<User> areaUsers, String type, Integer start, Integer rows) throws SQLException, ClassNotFoundException {
        ArrayList<Note> list = new ArrayList<>();
        conn = JdbcUtils.dbconn();
        for (User areaUser : areaUsers) {
            String sql = "select * from yonghu where uid='" + areaUser.getUid() + "' and style='" + 0 + "' limit " + start + "," + rows + "";
            ResultSet rs = JdbcUtils.selectsql(sql);
//            while (true) {
//                if (!rs.next()) break;
//                Note note = new Note();
//                note.setStyle(rs.getInt("style"));
//                note.setNid(rs.getInt("nid"));
//                note.setTitle(rs.getString("title"));
//                note.setContent(rs.getString("content"));
//                note.setType(rs.getString("type"));
//                note.setUsename(rs.getString("usename"));
//                note.setLikes(rs.getInt("likes"));
//                note.setFavourite(rs.getInt("favourite"));
//                list.add(note);
//            }
            rs.last();
            int row = rs.getRow();
            rs.beforeFirst();
            System.out.println(rs.getRow());
            do {
                row--;
                Note note = new Note();
                note = (Note) DataUtils.dataEncapsulation(Class.forName("pojo.Note"), rs);
                list.add(note);
            } while (row > 0);
            JdbcUtils.closeconn(rs, null, conn);
        }

        return list;
    }


    public ArrayList<User> areaUsersList(String type, Integer start, Integer rows) throws SQLException, ClassNotFoundException {
        ArrayList<User> areaUsers = new ArrayList<>();
        ArrayList<User> userList = new ArrayList<>();
        UserService userService = new UserServiceImpl();
        areaUsers = userService.areaUserList(type);
        ArrayList<User> list = new ArrayList();
        for (Integer i = start, j = 0; i < areaUsers.size(); i++, j++) {
            if (j == 5) break;
            userList.add(areaUsers.get(i));
        }
        return userList;
    }

    public Integer afterSearchTotalNotes(String content) throws SQLException, ClassNotFoundException {
        Integer number = 0;
        ArrayList<NoteLabels> searchNoteLabels = new ArrayList<>();
        NoteLabelsService noteLabelsService = new NoteLabelsServiceImpl();
        searchNoteLabels = noteLabelsService.searchAllLabels(content);
        for (NoteLabels searchNoteLabel : searchNoteLabels) {
            number++;
        }
        return number;
    }

    public Integer afterExatSearchTotalNotes(String labelSearch) throws SQLException, ClassNotFoundException {
        Integer number = 0;
        NoteLabelsService noteLabelsService = new NoteLabelsServiceImpl();
        ArrayList<NoteLabels> exatSearchNoteLabels = new ArrayList<>();
        exatSearchNoteLabels = noteLabelsService.exatSearchAllLabels(labelSearch);
        for (NoteLabels exatSearchNoteLabel : exatSearchNoteLabels) {
            number++;
        }
        return number;
    }


    public ArrayList<Note> afterSearchNotesList(Integer start, Integer rows, String content) throws SQLException, ClassNotFoundException {
        ArrayList<NoteLabels> searchNoteLabels = new ArrayList<>();
        ArrayList<Note> list = new ArrayList<>();
        NoteDao noteDao = new NoteDaoImpl();
        NoteLabelsService noteLabelsService = new NoteLabelsServiceImpl();
        searchNoteLabels = noteLabelsService.searchAllLabels(content);
        for (NoteLabels searchNoteLabel : searchNoteLabels) {
            Note note = new Note();
            note = noteDao.findNote(searchNoteLabel.getNid());
            if (note.getContent() != null) list.add(note);
        }
        return list;
    }

    public ArrayList<Note> afterExatSearchNotesList(Integer start, Integer rows, String labelSearch) throws SQLException, ClassNotFoundException {
        ArrayList<NoteLabels> exatSearchNoteLabels = new ArrayList<>();
        ArrayList<Note> list = new ArrayList<>();
        NoteDao noteDao = new NoteDaoImpl();
        NoteLabelsService noteLabelsService = new NoteLabelsServiceImpl();
        exatSearchNoteLabels = noteLabelsService.exatSearchAllLabels(labelSearch);
        for (NoteLabels exatSearchNoteLabel : exatSearchNoteLabels) {
            Note note = new Note();
            note = noteDao.findNote(exatSearchNoteLabel.getNid());
            if (note.getContent() != null) list.add(note);
        }
        return list;
    }
}
